package cn.changgou.controller;

import cn.changgou.entity.Result;
import cn.changgou.entity.StatusCode;
import cn.changgou.file.FastDFSFile;
import cn.changgou.util.FastDFSUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {

    /*文件上传*/
    @RequestMapping(method = RequestMethod.POST)
    public Result upload(@RequestParam("file")MultipartFile file) throws Exception{
        //调用FastDFSUtil工具类将文件传入到FastDFS中
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),
                file.getBytes(),
                StringUtils.getFilenameExtension(file.getOriginalFilename())
        );
        String[] uploads = FastDFSUtil.upload(fastDFSFile);
        //拼接访问地址http://192.168.211.132:8080/group1/M00/00/00/wKjThF0DBzaAP23MAAXz2mMp9oM26.jpeg
        //String url = "http://192.168.211.132:8080/"+ uploads[0] +"/"+ uploads[1];
        String url = FastDFSUtil.getTrackerInfo() + "/" + uploads[0] +"/"+ uploads[1];
        return new Result(true, StatusCode.OK, "上传成功！", url);
    }
}
